﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.DirectShow.Filters;
using System.Windows.Media.DirectShow;
using System.Windows.Media.DirectShow.Avi;
using System.IO;
using System.Windows.Media.DirectShow.UI;
using System.Text;

namespace Test
{
    public partial class AviPage : UserControl
    {

        private FilterGraph graph;
        private AviSourceSplitter sourceFilter;
        private VideoAudioRenderer render;

        public AviPage()
        {
            InitializeComponent();

        }

        void sourceFilter_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (IPin p in sourceFilter.Pins)
                {
                    FourCC cc = p.MediaTypes.First().FourCC;
                    sb.AppendFormat("MajorType: {0}\r\n", p.MediaTypes.First().MajorType);
                    sb.AppendFormat("SubType: {0}\r\n", p.MediaTypes.First().SubType);
                    if (cc != "\0\0\0\0")
                        sb.AppendFormat("FourCC: {0}\r\n", cc);
                    sb.AppendLine("==========================================");
                }
                MessageBox.Show(sb.ToString());

                if (sourceFilter.Pins.SelectMany(p => p.MediaTypes).Any(p => p.SubType.Equals(MediaSubTypes.PCM)))
                    graph.ConnectFilters(sourceFilter, render, MediaMajorTypes.Audio);
                else
                    MessageBox.Show("Only PCM audio stream is supported");

                //if (sourceFilter.Pins.SelectMany(p => p.MediaTypes).Any(p => p.SubType.Equals(MediaSubTypes.ARGB) || p.SubType.Equals(MediaSubTypes.RGB24)))
                //    graph.ConnectFilters(sourceFilter, render, MediaMajorTypes.Video);
                //else
                //    MessageBox.Show("Only ARGB or RGB24 video stream is supported");

                graph.RunAsync();
            }
            else
            {
                MessageBox.Show(String.Format("Error: {0}", e.Error.Message));
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (graph != null)
                graph.StopAsync();

            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "AVI file (*.avi)|*.avi";
            if (o.ShowDialog().GetValueOrDefault())
            {
                graph = new FilterGraph();

                sourceFilter = new AviSourceSplitter();
                sourceFilter.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(sourceFilter_LoadCompleted);
                graph.AddFilter("source", sourceFilter);

                render = new VideoAudioRenderer();
                graph.AddFilter("render", render);

                sourceFilter.LoadAsync(o.File.OpenRead());
            }
        }
    }
}
